package com.wenwei.sharebox.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.bean.ProposalBean;
import com.wenwei.sharebox.bean.ShareGoodsBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.utils.UserStringUtils;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func4;
import rx.functions.FuncN;

/**
 * Created by Administrator on 2017/7/10.
 */
public class GainIntegralActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.share_buy_tv)
    TextView shareBuyTv;
    @Bind(R.id.share_zu_tv)
    TextView shareZuTv;
    @Bind(R.id.suggestion_ll)
    LinearLayout suggestionLl;
    @Bind(R.id.user_name_ip)
    EditText userNameIp;
    @Bind(R.id.user_phone_ip)
    EditText userPhoneIp;
    @Bind(R.id.user_address_ip)
    EditText userAddressIp;
    @Bind(R.id.goods_name_ip)
    EditText goodsNameIp;
    @Bind(R.id.share_ll)
    LinearLayout shareLl;
    @Bind(R.id.commit_btn)
    TextView commitBtn;
    @Bind(R.id.suggestion_et)
    EditText suggestionEt;
    @Bind(R.id.commit_btn_sug)
    TextView sugCommit;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_gain_intergal;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("赚取积分");
        changeSelect(1);//默认为选择第一个
            checkShareInput();
            checkSuggestionInput();
    }

    private void checkSuggestionInput(){
        suggestionEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!suggestionEt.getText().toString().equals("")){
                    sugCommit.setBackgroundResource(R.drawable.bc_share_select);
                }else {
                    sugCommit.setBackgroundResource(R.drawable.bc_click_false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void checkShareInput(){
        Observable<CharSequence> nameObservable = RxTextView.textChanges(userNameIp);
        Observable<CharSequence> phoneObservable = RxTextView.textChanges(userPhoneIp);
        Observable<CharSequence> addressObservable = RxTextView.textChanges(userAddressIp);
        Observable<CharSequence> goodsNameObservable = RxTextView.textChanges(goodsNameIp);
        Observable.combineLatest(nameObservable, phoneObservable,addressObservable,goodsNameObservable, new Func4<CharSequence, CharSequence, CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean call(CharSequence charSequence, CharSequence charSequence2
                    ,CharSequence charSequence3,CharSequence charSequence4) {
                return !charSequence.toString().equals("") && !charSequence2.toString().equals("")
                        && !charSequence3.toString().equals("")&& !charSequence4.toString().equals("");
            }
        }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                commitBtn.setEnabled(aBoolean);
                if(aBoolean){
                    commitBtn.setBackgroundResource(R.drawable.bc_share_select);
                }else{
                    commitBtn.setBackgroundResource(R.drawable.bc_click_false);
                }
            }
        });
    }

    @OnClick({R.id.base_back, R.id.share_buy_tv, R.id.share_zu_tv, R.id.commit_btn,R.id.commit_btn_sug})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.share_buy_tv:
                changeSelect(1);
                break;
            case R.id.share_zu_tv:
                changeSelect(2);
                break;
            case R.id.commit_btn:
                shareGoods();
                break;
            case R.id.commit_btn_sug:
                putProposal();
                break;
        }
    }


    private void changeSelect(int position){
        if (position == 1){
            //购买
            shareLl.setVisibility(View.VISIBLE);
            suggestionLl.setVisibility(View.GONE);
            shareBuyTv.setBackgroundResource(R.drawable.bc_share_select);
            shareBuyTv.setTextColor(Color.parseColor("#ffffff"));
            shareZuTv.setBackgroundResource(R.drawable.bc_share_normal);
            shareZuTv.setTextColor(Color.parseColor("#40CCCB"));
            commitBtn.setVisibility(View.VISIBLE);
            sugCommit.setVisibility(View.GONE);
        }else if (position == 2){
            //租用
            shareLl.setVisibility(View.GONE);
            suggestionLl.setVisibility(View.VISIBLE);
            shareBuyTv.setBackgroundResource(R.drawable.bc_share_normal);
            shareBuyTv.setTextColor(Color.parseColor("#40CCCB"));
            shareZuTv.setBackgroundResource(R.drawable.bc_share_select);
            shareZuTv.setTextColor(Color.parseColor("#ffffff"));
            commitBtn.setVisibility(View.GONE);
            sugCommit.setVisibility(View.VISIBLE);
        }
    }


    //共享物品
    private BaseSubscriber<ShareGoodsBean> shareGoodsSubscriber;
    private void shareGoods(){
        shareGoodsSubscriber = new BaseSubscriber<ShareGoodsBean>(mContext,true,"请稍后...") {
            @Override
            public void onSuccess(ShareGoodsBean shareGoodsBean) {
                showToast("感谢您的共享，稍后工作人员会与你取得联系");
                finish();
            }
        };
        try{
            JSONObject jb = new JSONObject();
            jb.put("detailedAddress",userAddressIp.getText().toString());
            jb.put("phone",userPhoneIp.getText().toString());
            jb.put("shopDescription",goodsNameIp.getText().toString());
            jb.put("userId",UserStringUtils.getUserId(mContext));
            jb.put("username",userNameIp.getText().toString());
            ApiManager.getInstance().shareGoods(shareGoodsSubscriber,jb.toString());
        }catch (Exception e){}

    }


    //建议
    private BaseSubscriber<ProposalBean> proposalBeanBaseSubscriber;
    private void putProposal(){
        proposalBeanBaseSubscriber = new BaseSubscriber<ProposalBean>(mContext,true,"请稍后...") {
            @Override
            public void onSuccess(ProposalBean proposalBean) {
                showToast("感谢您提出宝贵的建议，祝您生活愉快");
                finish();
            }
        };
        try{
            JSONObject jb = new JSONObject();
            jb.put("proposal",suggestionEt.getText().toString());
            jb.put("userId", UserStringUtils.getUserId(mContext));
            ApiManager.getInstance().proposal(proposalBeanBaseSubscriber,jb.toString());
        }catch (Exception e){}
    }
}
